package com.cardvalue.sys.tool;

import android.content.Context;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Retrofit;

/**
 * 获取IP地址
 * Created by cardvalue on 2016/4/15.
 */
public class Ip {
    private Retrofit retrofit;
    private  Context context;
    private Request request;
    OkHttpClient client=new OkHttpClient();
    public Ip(Context context){
        this.context=context;
         request=new Request.Builder()
                .url("http://ip.cn/")
                .build();
    }


   public void Ip(){
       retrofit = new Retrofit.Builder()
               .baseUrl("http://ip.cn/")
               .client(new OkHttpClient())
               .addConverterFactory(new Converter.Factory() {
                   @Override
                   public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
                       return new Converter<ResponseBody, String>() {
                           @Override
                           public String convert(ResponseBody value) throws IOException {
                               Utiltools.printE("==IP=",value.string()+"");
                               return value.string();
                           }
                       };
                   }
               })
               .build();
   }


    public void IpService(){
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String respo=response.body().string();
                String ip= respo.substring(respo.indexOf("<code>") +6, respo.indexOf("</code>"));
                Utiltools.printE("=====","======"+ip);
            }
        });
    }
}
